package com.bm.specialvehiclefordispatcher.logics;

import android.content.Context;

import com.bm.corelibs.logic.BaseLogic.NListener;
import com.bm.corelibs.utils.SharedPreferencesUtil;
import com.bm.specialvehiclefordispatcher.bean.BaseData;
import com.bm.specialvehiclefordispatcher.bean.Dispatcher;
import com.bm.specialvehiclefordispatcher.constants.Constant;
import com.bm.specialvehiclefordispatcher.constants.Urls;

public class LoginManager extends BaseManager {

	private SharedPreferencesUtil sp;
	private static LoginManager instance;

	public LoginManager() {
	}

	public LoginManager(Context context) {
		sp = new SharedPreferencesUtil(context, Constant.SpName);
	}

	public void getVCode(String mobile, NListener<BaseData> l) {
		logic.edit().resetAll().addParam("mobile", mobile).addParam("role", Constant.ROLE).setBaseClass(BaseData.class).setListener(l).setUrl(Urls.GET_VCODE);
		logic.doPost();
	}

	public void checkVCode(String mobile, String vCode, NListener<BaseData> l) {
		logic.edit().resetAll().addParam("mobile", mobile).addParam("vCode", vCode).setBaseClass(BaseData.class).setListener(l).setUrl(Urls.CHECK_VCODE);
		logic.doPost();
	}

	public void getVcodeOnFindPsw(String mobile, String type, NListener<BaseData> listener) {
		logic.edit().resetAll().addParam("mobile", mobile).addParam("type", type).addParam("role", Constant.ROLE).setBaseClass(BaseData.class).setListener(listener).setUrl(Urls.GET_VCODE);
		logic.doPost();
	}

	public void register(String mobile, String password, NListener<BaseData> l) {
		logic.edit().resetAll().addParam("mobile", mobile).addParam("password", password).addParam("role", Constant.ROLE).setBaseClass(BaseData.class).setListener(l).setUrl(Urls.REGISTER);
		logic.doPost();
	}

	public void login(String username, String password, NListener<BaseData> l) {
		logic.edit().resetAll().addParam("username", username).addParam("password", password).addParam("role", Constant.ROLE).setBaseClass(BaseData.class).setListener(l).setUrl(Urls.LOGIN);
		logic.doPost();
	}

	public void resetPassword(String disId, String oldPwd, String newPwd, NListener<BaseData> l) {
		logic.edit().resetAll().addParam("disId", disId).addParam("oldPwd", oldPwd).addParam("newPwd", newPwd).setBaseClass(BaseData.class).setListener(l).setUrl(Urls.RESET_PASSWORD);
		logic.doPost();
	}

	public void findBackPassword(String mobile, String newPassword, String role, NListener<BaseData> listener) {
		logic.edit().resetAll().addParam("mobile", mobile).addParam("newPassword", newPassword).addParam("role", role).setListener(listener).setBaseClass(BaseData.class).setUrl(Urls.FINDBACK_PASSWORD);
		logic.doPost();
	}

	public synchronized static LoginManager getInstance(Context context) {
		if (instance == null)
			instance = new LoginManager(context);
		return instance;
	}

	public void saveUserInfo(Dispatcher user) {
		if (sp == null)
			return;
		sp.saveString("uid", user.disId + "");
		sp.saveString("phone", user.phone + "");
		sp.saveString("name", user.name + "");
		sp.saveString("icon", user.icon + "");
		sp.saveString("id", user.id + "");
		sp.saveString("address", user.address + "");
	}

	public void clearUserInfo() {
		if (sp == null)
			return;
		sp.clear();
	}

	public String getId() {
		if (sp == null)
			return "";
		return sp.getStringByKey("id");
	}

	public String getIcon() {
		if (sp == null)
			return "";
		return sp.getStringByKey("icon");
	}

	public String getName() {
		if (sp == null)
			return "";
		return sp.getStringByKey("name");
	}

	public String getPhone() {
		if (sp == null)
			return "";
		return sp.getStringByKey("phone");
	}

	public String getUid() {
		if (sp == null)
			return "";
		return sp.getStringByKey("uid");
	}

	public String getAddress() {
		if (sp == null)
			return "";
		return sp.getStringByKey("address");
	}

	public Dispatcher getSavedUser() {
		Dispatcher user = new Dispatcher();
		try {
			user.id = Long.valueOf(getId());
		} catch (Exception e) {
			user.id = 0;
		}

		user.icon = getIcon();
		user.name = getName();
		user.phone = getPhone();
		user.disId = getUid();
		user.address = getAddress();
		return user;
	}

	public boolean isLogin() {
		if (sp == null)
			return false;
		return (getId() != null && getId().length() > 0);
	}
}
